/*
 * @Author: hrg
 * @Date: 2023-05-19 10:02:04
 * @LastEditTime: 2023-05-20 10:42:03
 * @FilePath: \study_go\go_gin\demo\main.go
 * @Description:
 */

package main

import (
	"database/sql"
	"fmt"

	_ "github.com/go-sql-driver/mysql"
)

// 定义用户的结构体：
type User struct {
	Id        int    `json:"id"`
	LoginName string `json:"login_name"`
	UserName  string `json:"user_name"`
	Email     string `json:"email"`
}

func main() {

	// 首先创建数据库对象信息：
	DBInfo := "root:root@tcp(localhost:3306)/tjs_purchase"
	db, err := sql.Open("mysql", DBInfo)
	if err != nil {
		fmt.Println("连接错误", err)
		return
	}
	defer db.Close()

	// 进行查询操作
	rows, err := db.Query("select id,login_name,user_name,email from sys_user")
	if err != nil {
		fmt.Println("查询错误1", err)
		return
	}

	//
scanln:
	if rows.Next() {
		person := new(User)
		err := rows.Scan(&person.Id, &person.LoginName, &person.UserName, &person.Email)
		if err != nil {
			fmt.Println("查询错误2", err)
			return
		}
		fmt.Println(person.Id, person.LoginName, person.UserName, person.Email)
		goto scanln
	}

}
